// HSS.Net.Dns.Records.RecordMX.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       RecordMX.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
using System;

namespace HSS.Net.Dns.Records
{
	/*
	3.3.9. MX RDATA format

		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
		|                  PREFERENCE                   |
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
		/                   EXCHANGE                    /
		/                                               /
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

	where:

	PREFERENCE      A 16 bit integer which specifies the preference given to
					this RR among others at the same owner.  Lower values
					are preferred.

	EXCHANGE        A <domain-name> which specifies a host willing to act as
					a mail exchange for the owner name.

	MX records cause type A additional section processing for the host
	specified by EXCHANGE.  The use of MX RRs is explained in detail in
	[RFC-974].
	*/
	/// <summary>
	/// 
	/// </summary>
	[System.CLSCompliant(false)]
	public class RecordMX : Record, IComparable
	{
		/// <summary>
		/// 
		/// </summary>
		[System.CLSCompliant(false)]
		public ushort PREFERENCE;
		/// <summary>
		/// 
		/// </summary>
		public string EXCHANGE;
		/// <summary>
		/// 
		/// </summary>
		public RecordMX(RecordReader rr)
		{
			PREFERENCE = rr.ReadUInt16();
			EXCHANGE = rr.ReadDomainName();
		}
		/// <summary>
		/// 
		/// </summary>
		public override string ToString()
		{
			return string.Format("{0} {1}", PREFERENCE, EXCHANGE);
		}
		/// <summary>
		/// 
		/// </summary>
		public int CompareTo(object objA)
		{
			RecordMX recordMX = objA as RecordMX;
			if (recordMX == null)
				return -1;
			else if (this.PREFERENCE > recordMX.PREFERENCE)
				return 1;
			else if (this.PREFERENCE < recordMX.PREFERENCE)
				return -1;
			else // they are the same, now compare case insensitive names
				return string.Compare(this.EXCHANGE, recordMX.EXCHANGE, true);
		}
	}
}